<?php
/**
 * Created by PhpStorm.
 * User: lih
 * Date: 2018/4/22
 * Time: 20:09
 */

namespace console\models\crawl;

use Yii;
use yii\db\Query;
use common\models\PostMedia;

class D177comicTask extends CrawlTask
{

    /**
     * @param $args array
     */
    public function actionIndex($args)
    {
        $ruleDataStr = $this->getRuleContent(CrawlTask::RULE_PATH_PREFIX . '/tupian/d177comic/entry.json');
        $ruleData = json_decode($ruleDataStr, true);
        $urlTest = sprintf($ruleData['listUrl'], '1');
        $this->getCrawlHelper()->useRule($ruleData);
        $this->startCrawlList($urlTest, 'd177comic/index');
    }

    /**
     * @param $crawlData array
     * @param $taskAction string
     */
    public function convertCrawlDataToPost($crawlData, $taskAction, &$postData)
    {
        parent::convertCrawlDataToPost($crawlData, $taskAction, $postData);

        try {
            if(!empty($crawlData)) {
                if(!empty($crawlData['attrs'])
                    && !empty($crawlData['detail'])) {
                    $attrs = $crawlData['attrs'];
                    $detail = $crawlData['detail'];
                    $dateline = @$crawlData['dateline'];
                    if(!empty($attrs['subject'])) {
                        $postData['subject'] = $attrs['subject'];
                    }
                    if(!empty($detail['imgs'])) {
                        $postData['mediaextra'] = array();
                        $postData['mediaextra']['mtype'] = PostMedia::MTYPE_IMAGE;
                        $postData['mediaextra']['images'] = $detail['imgs'];
                    }
                    if(!empty($dateline)) {
                        $postData['dateline'] = $dateline;
                    }
                }
            }
        } catch(\Exception $e) {
        }
    }
}
